from flask import request
import pymongo
from app.libs.error_code import Success
from app.libs.mongo_about.mongo_get_case_log import m_get_case_log
from app.libs.redprint import Redprint
from app.libs.save_log import format_log
from app.libs.token_auth import login_required
from app.libs.init import mongo
from app.validates.log import GetPageLog

api = Redprint('log')


@api.route('/<int:space_id>', methods=['GET'])
@login_required
def get_case_log(space_id):
    form = GetPageLog().validate_and_api()
    data = m_get_case_log(space_id, int(form.page.data), int(form.page_size.data))
    return Success(data=data)


@api.route('', methods=['GET'])
@login_required
def get_all_log():
    data = mongo.db.request_records.find().sort('create_time', pymongo.DESCENDING)
    return Success(data=format_log(data))


@api.route('/info/<task_id>', methods=['GET'])
@login_required
def get_task_log(task_id):
    data = [mongo.db.request_records.find_one({"task_id": task_id})]
    return Success(data=format_log(list(data)))
